<!--
  移动签批->公文详情->发文->更多操作->生成发文字号
-->
<template>
  <view>
    <van-popup closeable round v-model="showCompo" position="bottom" :style="{ height: '80%' }"
               @click-close-icon="toClose" @click-overlay="toClose">
      <van-row type="flex" justify="center">
        <van-col offset="1" span="8">
          <van-cell value="生成发文字号"/>
        </van-col>
      </van-row>
      <van-form @submit="onSubmit">
        <van-cell is-link title="字头" v-model="fwWord" @click="showZt = true"/>
        <van-action-sheet ref="docType" v-model="showZt" :actions="zt" cancel-text="取消"
                          close-on-click-action @select="ztSelect"/>
        <van-cell is-link title="选择年份" v-model="fwYear" @click="showFwYear = true"/>
        <van-action-sheet ref="fwYear" v-model="showFwYear" :actions="fwYearList" cancel-text="取消"
                          close-on-click-action @select="yearSelect"/>
        <van-field v-model="fwNum" label="公文字号" readonly input-align="right"/>
      </van-form>
      <view class="css">
        <van-row type="flex" justify="space-around">
          <van-col span="10">
            <van-button type="info" round plain hairline block @click="toClose">取消</van-button>
          </van-col>
          <van-col span="10">
            <van-button type="info" round block @click="onSubmit">确定</van-button>
          </van-col>
        </van-row>
      </view>
    </van-popup>
  </view>
</template>

<script>
export default {
  props: {
    showGenerate: {
      type: Boolean,
      default: false
    },
    generateParamObject: {
      type: Object
    }
  },
  watch: {
    generateParamObject: {
      handler(newObj, oldObj) {
        this.fwId = newObj.fwId
        this.fwWord = newObj.fwWord
        this.fwWordId = newObj.fwWordId
        if (newObj.fwYear == null || newObj.fwYear == '') {
          this.fwYear = this.dict.getCurrentYear()
        }
        if (newObj.fwWordId != null && newObj.fwWordId != '') {
          this.getMaxfwNum()
        }
        if (newObj.fwNum != null && newObj.fwNum != '') {
          this.fwNum = newObj.fwNum
        }
      },
      immediate: true,
      deep: true
    },
    showGenerate(val) {
      this.showCompo = val
    },
  },
  data() {
    return {
      showCompo: false,//是否显示组件
      fwId: '',//发文id
      fwWord: '',//字头
      fwWordId: '',//字号id
      fwYear: '',//
      fwNum: '',
      showZt: false,//显示字头下拉列表
      showFwYear: false,//显示年份下拉列表

      zt: [],
      fwYearList: [{
        name: '2021'
      }, {
        name: '2020'
      }, {
        name: '2019'
      }, {
        name: '2018'
      }],
    };
  },
  mounted() {
    this.fwYearList = this.dict.getYearArr()
    //回显
    this.getGwWordList('fw')//获取发文函的字头列表
  },
  methods: {
    //字头选择
    ztSelect(item) {
      this.fwWord = item.name
      this.fwWordId = item.wordId
      this.getMaxfwNum();
      this.showZt = false;
    },
    //年份选择
    yearSelect(item) {
      this.fwYear = item.name;
      this.getMaxfwNum();
      this.showFwYear = false;
    },
    onSubmit() {
      this.generateWord();
      this.$emit('closeGenerate', this.showGenerate);
    },
    toClose() {
      this.$emit('closeGenerate', this.showGenerate);
    },
    //生成发文字号接口
    generateWord() {
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/component/word/updatefwword?systemKey=` + this.cache.get('userInfo').systemKey + `&systemFlag=` + this.cache.get('userInfo').systemFlag),
        method: 'put',
        params: this.$http.adornParams({
          'fwId': this.fwId,
          'fwWord': this.fwWord,
          'fwWordId': this.fwWordId,
          'fwYear': this.fwYear,
          'fwNum': this.fwNum,
          'type': 'fw',
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast('生成发文字号成功')
          this.$emit('refreshBtnGroup', true)//通知父组件重新加载数据
        } else {
          this.$toast(data.message + ' code:' + data.code)
        }
      })
    },

    //获取字头列表
    /**
     * @param {Object} type   type=fw
     */
    getGwWordList(type) {
      const _this = this
      this.$httploading({
        url: this.$http.adornUrl('/caac/oa/component/word/getwordbyuserandtype'),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'type': 'fw'

        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data !== undefined && data.data.length > 0) {
            _this.zt = []//置空
            data.data.forEach((item) => {
              _this.zt.push({name: item.wordName, wordId: item.wordId})
            })
          }
        }
      })
    },

    /**
     * 获取发文字号年后面的号数
     * @param {Object} type   type=fw
     */
    getMaxfwNum() {
      const _this = this
      this.$http({
        url: this.$http.adornUrl('/caac/oa/component/word/getmaxfwnum'),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'fwWord': this.fwWordId,
          'fwYear': this.fwYear,
          'type': 'fw',
          'fwId': this.fwId

        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.fwNum = data.data.data
        }
      })
    },
  },
};
</script>

<style scoped>
	.css {
		text-align: center;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 3%;
	}
	/deep/ .van-cell__title {
		min-width: 30%;
	}
	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}
</style>
